"""
base_merge.py 将多个recall得到的数据合并在一起,并统计合并信息
"""
import threading


class BaseMerge(threading.Thread):
    def __init__(self, config):
        super().__init__()
        self.config = config
        self.args = None

    def init(self, *args):
        self.args = args

    def do_merge(self, global_resource, request_content, response_content):
        all_api = {}
        for retrieve, result in response_content.retrieve_result.items():
            for api in result:
                if api["api_id"] not in all_api:
                    all_api[api["api_id"]] = api
                    all_api[api["api_id"]]["retrieve_from"] = []
                all_api[api["api_id"]]["retrieve_from"].append(retrieve)

        response_content.store_value("merge_result", all_api)

    def run(self) -> None:
        if not self.args:
            print("merge service start without init")
            return
        self.do_merge(*self.args)
